// pages/login/index.js
const config = getApp().globalData.config
const T = getApp().globalData.T
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userId: 0,
    userType: 0,
    code: '',
    token: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let {
      userId,
      userType
    } = options;
    this.data.userId = userId ? userId : 0;
    this.data.userType = userType ? userType : 0;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    
  },

  isComplete() {
    T.req({
      loadTitle: '加载中...',
      url: config.api + 'completion',
      method: 'GET',
      data: {
        token: this.data.token
      },
      success: (res) => {
          if (res.isCompletion) {
            wx.navigateTo({
              url: '/pages/login/status/status',
            })
          } else {
            wx.redirectTo({
              url:'/pages/login/base-info/info'
            })
          }
      }
    })
  },

  getLaunchImg() {
    T.req({
      loadTitle: '加载中...',
      url: config.api + 'start_page',
      method: 'GET',
      success: (res) => {
        if (res.imageUrl) {
          this.setData({ imageUrl: res.imageUrl, isShowImageUrl: true });
          setTimeout(() => {
            this.setData({ isShowImageUrl: false })
          }, 1000 * 4)
        }
      }
    })
  },

  loginTcode() {
    let that = this;
    wx.login({
      success: function(e) {
        that.setData({
          code: e.code
        });
        console.log(e);
      },
      fail: function(e) {
        console.log('wx:login 登录失败');
      }
    })
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.loginTcode();
    this.getLaunchImg();
  },

  onShareAppMessage: function() {

  },

  onFindWork() {
    wx.navigateToMiniProgram({
      appId: 'wx5316719f8ac7148d',
      extraData: {
        userId: this.data.userId,
        userType: this.data.userType
      }
    })
  },

  onGetUserinfo(e) {
    if (e.detail.errMsg != 'getUserInfo:ok') {
      wx.showToast({
        title: '授权失败,请重新授权',
        icon: 'none'
      })
      return;
    }
    // console.log(JSON.stringify({
    //   code: this.data.code,
    //   encryptedData: e.detail.encryptedData,
    //   iv: e.detail.iv,
    //   userId: this.data.userId,
    //   userType: this.data.userType,
    // }, null, '\t'));
    T.req({
      loadTitle: '加载中...',
      url: config.api + 'login',
      method: 'POST',
      data: {
        code: this.data.code,
        encryptedData: e.detail.encryptedData,
        iv: e.detail.iv,
        userId: this.data.userId,
        userType: this.data.userType,
      },
      success: (res) => {
        getApp().globalData.token = res.token;
        getApp().globalData.userId = res.userId;
        getApp().globalData.openId = res.openid;
        this.data.token = res.token;
        if (!res.isBind) { // 还没有绑定手机
          wx.redirectTo({
            url: './auth-code/code',
          })
          return;
        }
        
        this.isComplete();
      },
      error: (res) => {
        console.log(res);
        T.toast({
          id: '#toast',
          text: '授权失败,请重新授权',
          success: () => {
              this.loginTcode();
          }
        })
      }
    });
  }
})